<?php
/**
 * 管理员后台会员操作类
 * @author           袁志蒙  
 * @license          http://www.yzmcms.com
 * @lastmodify       2018-01-22
 */
 
defined('IN_YZMPHP') or exit('Access Denied'); 
yzm_base::load_controller('common', 'admin', 0);
yzm_base::load_common('function/hr.func.php');
yzm_base::load_common('class/hr_xlswriter.class.php');
yzm_base::load_sys_class('page','',0);

class hr_member extends common{

	
	/**
	 * 员工列表
	 */	
	public function member_list(){
		$of = input('get.of');
		$or = input('get.or');
		$of = in_array($of, array('m.id','m.in_time','m.out_time','ml.is_ss','ml.money','ml.is_second','m.department_id','m.leave_days,m.status','m.leave_express')) ? $of : 'm.id';
		$or = in_array($or, array('ASC','DESC')) ? $or : 'DESC';
		
		$get = $_GET;
		$where = ' m.status = 1';
		$dosubmit = $get['dosubmit'] ?? '';
		$department_id = $get['department_id'] ?? '';
		$is_housing = $get['is_housing']??'';
		$is_meal = $get['is_meal']??'';
		$education = $get['education']??'';
		$is_agree = $get['is_agree']??'';
		if($dosubmit == 1){
			$where = '1=1';
			if(isset($get['status'])&&$get['status']!=''){
				$where .= ' AND m.status = '.$get['status'];
			}
			if(isset($get['name'])&&trim($get['name'])!=''){
				$where .= ' AND m.name LIKE "%'.trim($get['name']).'%"';
			}
			if(isset($get['mobile'])&&trim($get['mobile'])!=''){
				$where .= ' AND m.mobile LIKE "%'.trim($get['mobile']).'%"';
			}
			if(isset($get['native_place'])&&trim($get['native_place'])!=''){
				$where .= ' AND m.native_place LIKE "%'.trim($get['native_place']).'%"';
			}
			if($department_id){
				$list = D('hr_department')->field('id')->where('parentid='.$get['department_id'])->select();
				//print_r($list);exit;
				if(!$list){
					$where .= ' AND m.department_id = '.$get['department_id'];
				}else{
					$ids = array_column($list, 'id');
					$where .= ' AND m.department_id in ('.implode(',',$ids).')';	
				}	
			}
			if($education!==''){
				$where .= " AND m.education='".$education."'";
			}
			if($is_agree==1){
				$where .= " AND m.agree_no<>''";
			}
			if($is_agree==2){
				$where .= " AND m.agree_no=''";
			}
		}
		$member = D('hr_member');
		$total = $member->alias('m')//->join('yzm_hr_money_list ml ON m.money_id=ml.id AND ml.is_action = 1','left')
				->where($where)->total();
		$page = new page($total, 15);
		$data = $member->alias('m')//->field('m.*,ml.money,ml.housing_money,ml.meal_money,ml.is_ss,ml.is_second,ml.remark,ml.is_ss')
			//->join('yzm_hr_position p ON m.position_id = p.id','left')
			//->join('yzm_hr_department d ON m.department_id=d.id','left')
			->where($where)->order("$of $or")->limit($page->limit())->select();
		$tree = yzm_base::load_sys_class('tree');
		$department_list = D('hr_department')->where(['display'=>1])->order('listorder ASC,id ASC')->limit(1000)->select();
		$array = array();
		foreach($department_list as $v) {
			$v['selected'] = $v['id'] == $department_id ? 'selected' : '';
			$array[] = $v;
		}
		$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
		$tree->init($array);
		$select_menus = $tree->get_tree(0, $str);
		foreach($data as &$v){
			$number = substr($v['card_id'], strlen($v['card_id']) - 2, 1);
			if ($number % 2 == 0) {//偶数女
				$v['img'] = STATIC_URL.'images/women.png';
				$v['sex'] = '女';
			} else {//奇数男
				$v['img'] = STATIC_URL.'images/man.png';
				$v['sex'] = '男';
			}
			$v['years'] = 0;
			if($v['status']==1){
				$v['years'] = get_date_distance($v['in_time']);
			}
			
			if($v['card_id']){
				$v['age'] = get_age($v['card_id']);
			}else{
				$v['age'] = '-';
			}
			$position = position($v['position_id']);
			$v['position_name'] = $position['name'];
			$v['position_leave_days'] = $position['leave_days'];
			$v['money_wait_action'] = D('hr_money_list')->where(['is_action'=>2,'member_id'=>$v['id']])->find();
			$money = D('hr_money_list')->where(['member_id'=>$v['id'],'is_action'=>1])->find();
			//print_r($money);exit;
			unset($money['id']);
			$v = array_merge($v,$money);
		}
		include $this->admin_tpl('hr/member_list');
	}
	
	public function member_info(){
		$where = 'm.id = '.$_GET['id'];
		$member = D('hr_member');
		$data = $member->alias('m')//->field('m.*,ml.money,ml.housing_money,ml.meal_money,ml.is_ss,ml.is_second,ml.remark')
			//->join('yzm_hr_money_list ml ON m.money_id=ml.id','left')
			//->join('yzm_hr_position p ON m.position_id = p.id','left')
			//->join('yzm_hr_department d ON m.department_id=d.id','left')
			->where($where)->find();
		$number = substr($data['card_id'], strlen($data['card_id']) - 2, 1);
		if ($number % 2 == 0) {//偶数女
			$data['img'] = STATIC_URL.'images/women.png';
			$data['sex'] = '女';
		} else {//奇数男
			$data['img'] = STATIC_URL.'images/man.png';
			$data['sex'] = '男';
		}
		$data['years'] = get_date_distance($data['in_time']);
		$position = position($data['position_id']);
		$data['position_name'] = $position['name'];
		$data['position_leave_days'] = $position['leave_days'];
		$money = D('hr_money_list')->where(['member_id'=>$_GET['id'],'is_action'=>1])->find();
		unset($money['id']);
		$data = array_merge($data,$money);
		if($data['card_id']){
			$data['age'] = get_age($data['card_id']);
		}else{
			$data['age'] = '-';
		}
		include $this->admin_tpl('hr/member_info');
	}
	
	
	
	/**
	 * 添加用户
	 */	
	public function member_add(){
		if(isset($_GET['id'])){
			//编辑员工，获取员工资料
			$where = 'm.id = '.$_GET['id'];
			$member = D('hr_member');
			$data = $member->alias('m')->field('m.*,ml.money,ml.housing_money,ml.meal_money,ml.is_ss,ml.is_second,ml.remark,p.name as position_name,p.leave_days,d.id as department_id')
				->join('yzm_hr_money_list ml ON m.money_id=ml.id','left')
				->join('yzm_hr_position p ON m.position_id = p.id','left')
				->join('yzm_hr_department d ON m.department_id=d.id','left')
				->where($where)->find();
			$position_list = D('hr_position')->where(['department_id '=>$data['department_id']])->select();
		}else{
			//添加员工，默认数据
			$data = [
				'department_id' => '',
				'name'=>'',
				'mobile'=>'',
				're_name'=>'',
				're_mobile'=>'',
				'position'=>'',
				'education'=>'',
				'major'=>'',
				'native_place'=>'',
				'money'=>'',
				'meal_money'=>'',
				'housing_money'=>'',
				'is_ss'=>0,
				'card_id'=>'',
				'address'=>'',
				'bank_name'=>'',
				'bank_code'=>'',
				'bank_account'=>'',
				'in_time'=>'',
				'out_time'=>'',
				'money_id'=>0,
				'id'=>0,
				'is_ss'=>0,
				'agree_no'=>'',
				'is_second'=>'',
				'remark'=>'',
				'status'=>1,
			];
		}
		if(isset($_POST['dosubmit'])){
			$data = [];
			$post =$_POST;
			//数据验证
			if(!trim($post['name'])) return_json(array('status'=>0,'message'=>'请输入姓名！'));
			if(!trim($post['department_id'])) return_json(array('status'=>0,'message'=>'请选择部门！'));
			if(!trim($post['position_id'])) return_json(array('status'=>0,'message'=>'请选择岗位！'));
			if(!isset($post['money'])&&!$post['id']) return_json(array('status'=>0,'message'=>'请输入工资！'));
			if(!trim($post['re_name'])) return_json(array('status'=>0,'message'=>'请输入紧急联系人！'));
			if(!trim($post['re_mobile'])) return_json(array('status'=>0,'message'=>'请输入紧急联系人电话！'));
			if(!trim($post['in_time'])) return_json(array('status'=>0,'message'=>'请选择入职时间！'));
			if(!is_card_no($post['card_id'])) return_json(array('status'=>0,'message'=>'身份证号码错误！'));
			
			//数据格式化
			$data['name'] = trim($post['name']);
			$data['department_id'] = $post['department_id'];
			$data['position_id'] = $post['position_id'];
			$data['card_id'] = trim($post['card_id']);
			$data['address'] = trim($post['address']);
			$data['bank_name'] = trim($post['bank_name']);
			$data['bank_code'] = trim($post['bank_code']);
			//银行户名称和员工姓名一样
			if(isset($post['is_same'])&&$post['is_same']==1){
				$data['bank_account'] = $data['name'];
			}else{
				$data['bank_account'] = trim($post['bank_account']);
			}
			$data['in_time'] = strtotime($post['in_time']);
			$data['out_time'] = strtotime($post['out_time']);
			$data['education'] = $post['education'];
			$data['native_place'] = $post['native_place'];
			$data['major'] = trim($post['major']);
			$data['mobile'] = trim($post['mobile']);
			$data['re_name'] = trim($post['re_name']);
			$data['re_mobile'] = trim($post['re_mobile']);
			$data['agree_no'] = trim($post['agree_no']);
			$data['add_time'] = time();
			//设置离职变更在职状态
			$data['status'] = $post['status'];
			$years = get_date_distance($data['in_time']);
			/*
			//匹配年假天数
			$position = D('hr_position')->where(['id'=>$data['position_id']])->find();
			$data['leave_days'] = $position['leave_days'];
			//工龄
			if($years>=1){
				$data['leave_expire'] = strtotime('+'.($years+1).' years',$data['in_time'])-86400;
			}
			*/
			
			if($post['id']){
				$result = D('hr_member')->update($data,['id'=>$post['id']]);
				$member_id = $post['id'];
			}else{
				$result = D('hr_member')->insert($data, true);
				$member_id = $result;
			}
			if($result!==false){
				if(!$post['money_id']){
					$remark = trim($post['remark']) ?  trim($post['remark']) : '初始化';
					$money_id = D('hr_money_list')->insert([
						'member_id'=>$member_id,
						'money'=>trim($post['money']),
						'add_time'=>time(),
						'action_time'=>$data['in_time'],
						'remark'=>trim($remark),
						'is_second'=>$post['is_second']??0,
						'housing_money'=>$post['housing_money']?trim($post['housing_money']):0,
						'meal_money'=>$post['meal_money']?trim($post['meal_money']):0,
						'is_action'=>1,
						'is_ss'=>$post['is_ss']??''
					],true);
					D('hr_member')->update(['money_id'=>$money_id,'last_money_id'=>$money_id],['id'=>$member_id]);
				}
				//计算年假
				$result = calc_member_leave($member_id);
				if($result){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>'计算年假失败！'));
				}

				return_json(array('status'=>1,'message'=>L('operation_success')));
			}else{
				return_json(array('status'=>0,'message'=>L('operation_failure')));
			}
		}
		
		$tree = yzm_base::load_sys_class('tree');
		$department_list = D('hr_department')->where(['display'=>1])->order('listorder ASC,id ASC')->limit(1000)->select();
		$array = array();
		foreach($department_list as $v) {
			$v['selected'] = $v['id'] == $data['department_id'] ? 'selected' : '';
			$array[] = $v;
		}
		$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
		$tree->init($array);
		$select_menus = $tree->get_tree(0, $str);

		include $this->admin_tpl('hr/member_add');
	}
	
	/**
	 * 根据部门获取岗位
	 */
	public function get_position(){
		if(isset($_GET['department_id'])){
			$position_list = D('hr_position')->where(['department_id '=>$_GET['department_id']])->select();
			if($position_list){
				return_json(array('status'=>1,'message'=>L('operation_success'),'data'=>$position_list));
			}else{
				return_json(array('status'=>0,'message'=>''));
			}
		}
	}
	
	/**
	 * 导出下载
	 */	
	public function download(){
		$type = $_GET['type'] ?? '';
		if(!$type){
			exit('参数错误！');
		}
		//离职不超过60天的一样导出
		/*
		$where = [
			'ml.is_action'=>1,
		];*/
		$where = '1=1';
		if($type == 'bank' || $type == 'last_bank'){
			$where .= ' AND m.status = 1 or m.out_time>'.(time()-86400*60);
		}
		$list = D('hr_member')->alias('m')//->field('m.*,ml.money,ml.housing_money,ml.meal_money,ml.is_ss,ml.is_second,ml.remark,ml.is_ss')
			//->join('yzm_hr_money_list ml ON m.money_id=ml.id','left')
			//->join('yzm_hr_position p ON m.position_id = p.id','left')
			//->join('yzm_hr_department d ON m.department_id=d.id','left')
			->where($where)->order("m.status desc,m.department_id asc,m.out_time desc,m.id asc")->select();
		$data = [];
		foreach($list as $key => $val){
			$insert_data = [];
			$insert_data['id'] = $val['id'];
			$insert_data['member_name'] = $val['name'];
			$insert_data['department_name'] = department($val['department_id']);
			$position = position($val['position_id']);
			$insert_data['position_name'] = $position['name'];
			$insert_data['status_text'] = $val['status'] ? '在职':'离职';
			$insert_data['in_time'] = $val['in_time'];
			$insert_data['out_time'] = $val['out_time'] ? $val['out_time'] : '-';
			$insert_data['years'] = 0;
			$insert_data['years'] = get_date_distance($val['in_time']).'年';
			
			if($type == 'all'){
				$insert_data['leave_days'] = 0;
				if($val['status']){
					
					if($insert_data['years']>=1){
						$insert_data['leave_days'] = $val['leave_days'];
					}
				}
				if($position['leave_days']<1){
					$insert_data['leave_expire'] = '无年假';
				}elseif($insert_data['years']>=1){
					$insert_data['leave_expire'] = $val['leave_expire'];
				}else{
					$insert_data['leave_expire'] = '工龄不够';
				}
			}
			if($type == 'last_bank'){
				$money = D('hr_money_list')->where(['member_id'=>$val['id'],'id'=>$val['last_money_id']])->find();
			}else{
				$money = D('hr_money_list')->where(['member_id'=>$val['id'],'is_action'=>1])->find();
			}
			

			$insert_data['money'] = $money['money'];
			$insert_data['housing_money'] = $money['housing_money'] ? $money['housing_money'] : '-';
			$insert_data['meal_money'] = $money['meal_money'] ? $money['meal_money'] : '-';
			$insert_data['is_ss'] = $money['is_ss'] ? '已购买' : '-';
			$insert_data['remark'] = htmlspecialchars_decode($money['remark']);
			
			$insert_data['bank_name'] = $val['bank_name'];
			$insert_data['bank_code'] = $val['bank_code'];
			$insert_data['bank_account'] = $val['bank_account'];
			
			
			if($type == 'all'){
				$number = substr($val['card_id'], strlen($val['card_id']) - 2, 1);
				if ($number % 2 == 0) {//偶数女
					$insert_data['sex'] = '女';
				} else {//奇数男
					$insert_data['sex'] = '男';
				}
				if($val['card_id']){
					$insert_data['age'] = get_age($val['card_id']);
				}else{
					$insert_data['age'] = '-';
				}
				$insert_data['mobile'] = $val['mobile'];
				$insert_data['native_place'] = $val['native_place'];
				$insert_data['agree_no'] = $val['agree_no'];
				$insert_data['card_id'] = $val['card_id'];
				$insert_data['address'] = $val['address'];
				$insert_data['education'] = $val['education'];
				$insert_data['major'] = $val['major'] ? $val['major'] : '-';
				$insert_data['re_name'] = $val['re_name'];
				$insert_data['re_mobile'] = $val['re_mobile'];
				$insert_data['add_time'] = $val['add_time'];	
			}
			$data[] = $insert_data;
		}
		//print_r($data);exit;
		$file_names = ['all'=>'全部员工信息表','bank'=>'员工工资及银行信息表','last_bank'=>'上个月员工工资及银行信息表'];
		$excel = new hr_xlswriter($type,1,$file_names[$type]);
		$excel->insert_data($data);
		$excel->output();
	}
	
	public function member_delete(){
		if($_POST && is_array($_POST['ids'])){
			$db = 'hr_member';
			if(D($db)->delete($_POST['ids'],true)){
				return_json(array('status'=>1,'message'=>L('operation_success')));
			}else{
				return_json(array('status'=>0,'message'=>L('operation_failure')));
			}
		}
	}

}